package com.dmy.common.utils.qr_code;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

/**
 * create by huahuadelei for 2020/1/5
 */
public class QRCodeHelper {

    private static final QRCodeHelper QR_CODE_HELPER = new QRCodeHelper();

    private QRCodeHelper() {
        formatWriter = new MultiFormatWriter();
        formatReader = new MultiFormatReader();
    }

    public static QRCodeHelper getInstance() {
        return QR_CODE_HELPER;
    }

    private final MultiFormatWriter formatWriter;
    private final MultiFormatReader formatReader;

    /**
     * 生成二维码
     *
     * @param content
     * @param config
     * @param output
     * @throws WriterException
     * @throws IOException
     */
    public final void generateQrCodeToStream(String content, QRCodeConfig config, OutputStream output) throws WriterException, IOException {
        BitMatrix bitMatrix = formatWriter.encode(content, BarcodeFormat.QR_CODE, config.getWidth(), config.getHeight());
        if (config.isLogo()) {
            BufferedImage bufferedImage = getBufferedImage(bitMatrix, config);
            setMatrixLogo(bufferedImage, config);
            ImageIO.write(bufferedImage, config.getImgFormat(), output);
        } else {
            MatrixToImageWriter.writeToStream(bitMatrix, config.getImgFormat(), output);
        }
    }

    /**
     * 识别二维码
     *
     * @return
     */
    public final String identifyQrCodeAsString() {
//        formatReader.coder();
        return "";
    }

    /**
     * 将BitMatrix对象转换成 BufferedImage
     * @param bitMatrix
     * @param config
     * @return
     */
    private BufferedImage getBufferedImage(BitMatrix bitMatrix, QRCodeConfig config) {
        BufferedImage image = new BufferedImage(config.getWidth(), config.getHeight(), BufferedImage.TYPE_INT_ARGB);
        final int BlackColor = 0xFF000000; // 黑色
        final int WhiteColor = 0xFFFFFFFF; // 白色
        for (int x = 0; x < config.getWidth(); x++) {
            for (int y = 0; y < config.getHeight(); y++) {
                // 相当于把二维码对象转换成了buffered bitMatrix.get(x, y) 返回二维码true，说明该坐标有前景内容，用黑色绘制，没有就用白色绘制
                image.setRGB(x, y, (bitMatrix.get(x, y) ? BlackColor : WhiteColor));
            }
        }
        return image;
    }



    public static BufferedImage setMatrixLogo(BufferedImage matrixImage, QRCodeConfig config) throws IOException {
        /**
         * 读取二维码图片，并构建绘图对象
         */
        Graphics2D g2 = matrixImage.createGraphics();
        int matrixWidth = matrixImage.getWidth();
        int matrixHeigh = matrixImage.getHeight();
        BufferedImage logo = ImageIO.read(config.getCodeLogoConfig().getLogoStream());//读取logo图片
        //开始绘制图片前两个、/5*2/5*2 后/5
        g2.drawImage(logo, matrixWidth / 5 * 2, matrixHeigh / 5 * 2, matrixWidth / 5, matrixHeigh / 5, null);
        //绘制边框
        BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        // 设置笔画对象
        g2.setStroke(stroke);
        //指定弧度的圆角矩形
        RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth / 5 * 2, matrixHeigh / 5 * 2, matrixWidth / 5, matrixHeigh / 5, 20, 20);
        g2.setColor(Color.white);
        // 绘制圆弧矩形
        g2.draw(round);

        //设置logo 有一道灰色边框
        BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        // 设置笔画对象
        g2.setStroke(stroke2);
        RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth / 5 * 2 + 2, matrixHeigh / 5 * 2 + 2, matrixWidth / 5 - 4, matrixHeigh / 5 - 4, 20, 20);
        g2.setColor(new Color(128, 128, 128));
        g2.draw(round2);// 绘制圆弧矩形
        g2.dispose(); //执行刷出返回带logo二维码
        matrixImage.flush();
        return matrixImage;

    }

}
